<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户昵称" prop="personUname">
        <el-input
          v-model="queryParams.personUname"
          placeholder="请输入用户昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户姓名" prop="personName">
        <el-input
          v-model="queryParams.personName"
          placeholder="请输入用户姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户性别" prop="personGender">
        <el-select
          v-model="queryParams.personGender"
          placeholder="用户性别"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户年龄" prop="personAge">
        <el-input
          v-model="queryParams.personAge"
          placeholder="请输入用户年龄"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话号码" prop="personTel">
        <el-input
          v-model="queryParams.personTel"
          placeholder="请输入用户电话号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="personNumber">
        <el-input
          v-model="queryParams.personNumber"
          placeholder="请输入用户身份证号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="个性签名" prop="personUtext">
        <el-input
          v-model="queryParams.personUtext"
          placeholder="请输入用户个性签名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="居住地址" prop="personAddress">
        <el-input
          v-model="queryParams.personAddress"
          placeholder="请输入用户居住地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fuw:person:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fuw:person:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fuw:person:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['fuw:person:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="personList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户编号" align="center" prop="personId" :show-overflow-tooltip='true'/>
      <el-table-column label="用户昵称" align="center" prop="personUname" :show-overflow-tooltip='true'/>
      <el-table-column align="center" width="100" label="用户头像">
        <template scope="scope">
          <img :src="scope.row.personUphoto" style="width:80px;height:80px;border:none;border-radius:500px;"
               onerror="this.src='http://localhost/dev-api/profile/upload/2021/12/16/2d1cde4d-3ba7-407f-82e1-0663e24318e4.jpg'"/>
        </template>
      </el-table-column>
      <el-table-column label="用户姓名" align="center" prop="personName" />
      <el-table-column label="用户性别" align="center" prop="personGender" :formatter="stateFormat"/>
      <el-table-column label="用户年龄" align="center" prop="personAge" />
      <el-table-column label="用户电话号码" align="center" prop="personTel" :show-overflow-tooltip='true'/>
      <el-table-column label="用户身份证号码" align="center" prop="personNumber" :show-overflow-tooltip='true'/>
      <el-table-column label="用户个性签名" align="center" prop="personUtext" :show-overflow-tooltip='true'/>
      <el-table-column label="用户居住地址" align="center" prop="personAddress" :show-overflow-tooltip='true'/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fuw:person:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fuw:person:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="用户昵称" prop="personUname">
          <el-input v-model="form.personUname" placeholder="请输入用户昵称" />
        </el-form-item>

        <el-form-item label="图像预览" prop="personUphoto">
          <el-upload :file-list="photoNow"
                     :limit="1"
                     :http-request="((val)=>{return myUpload(val)})"
                     :on-preview="handlePictureCardPreview"
                     :on-remove="((file,fileList)=>{return handleRemove(file,fileList)})"
                     :before-upload="handleBeforeUpload"
                     action=""
                     list-type="picture-card">
            <el-footer style="padding-right: 0px !important;padding-left: 0px !important;">请上传照片</el-footer>
            <!-- 点击进行图片放大预览  -->
            <i class="el-icon-plus" />
          </el-upload>
          <!-- 图片放大预览显示弹出框  -->
          <el-dialog :visible.sync="phone" :close-on-click-modal="false" :append-to-body="true">
            <img v-if="form.personUphoto" :src="form.personUphoto" width="100%" alt="">
          </el-dialog>

        </el-form-item>

        <el-form-item label="用户姓名" prop="personName">
          <el-input v-model="form.personName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="用户性别" prop="personGender">
          <el-radio-group v-model="form.personGender">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
            <el-radio :label="0">未知</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="用户年龄" prop="personAge">
          <el-input v-model="form.personAge" placeholder="请输入用户年龄" />
        </el-form-item>
        <el-form-item label="用户电话号码" prop="personTel">
          <el-input v-model="form.personTel" placeholder="请输入用户电话号码" />
        </el-form-item>
        <el-form-item label="用户身份证号码" prop="personNumber">
          <el-input v-model="form.personNumber" placeholder="请输入用户身份证号码" />
        </el-form-item>
        <el-form-item label="用户个性签名" prop="personUtext">
          <el-input v-model="form.personUtext" placeholder="请输入用户个性签名" />
        </el-form-item>
        <el-form-item label="用户居住地址" prop="personAddress">
          <el-input v-model="form.personAddress" placeholder="请输入用户居住地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPerson, getPerson, delPerson, addPerson, updatePerson, exportPerson } from "@/api/fuw/person";

export default {
  name: "Person",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 图片上传
      formData: "",
      photoNow: [],
      phone: false,
      // 删除图片
      deletePhoto: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息表格数据
      personList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 性别数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        personUname: null,
        personUphoto: null,
        personName: null,
        personGender: null,
        personAge: null,
        personTel: null,
        personNumber: null,
        personUtext: null,
        personAddress: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_user_sex").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 性别判断修改 */
    stateFormat(row, index){
      if (row.personGender == 1) {
        return "男";
      } else if(row.personGender == 2){
        return "女";
      } else if(row.personGender == 0){
        return "未知";
      }},
    /** 查询用户信息列表 */
    getList() {
      this.loading = true;
      listPerson(this.queryParams).then(response => {
        this.personList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.deletePhoto = false;
      // 清除图片
      this.photoNow = [];
      this.form = {
        personId: null,
        personUname: null,
        personUphoto: null,
        personName: null,
        personGender: null,
        personAge: null,
        personTel: null,
        personNumber: null,
        personUtext: null,
        personAddress: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.personId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const personId = row.personId || this.ids
      getPerson(personId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户信息";
        if(this.form.personUphoto != null && this.form.personUphoto != ''){
          this.photoNow = [{name: this.form.personUphoto.split('/').pop(), url: this.form.personUphoto}];
        }
      });
    },
    // 图片上传之前的判断
    handleBeforeUpload(file) {
      const isIMAGE = file.type === 'image/jpg' || file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt1M = file.size / 1024 / 500 < 1
      if (!isIMAGE) {
        this.$message.error('上传文件只能是jpg或png图片格式!')
      }
      if (!isLt1M) {
        this.$message.error('上传文件大小不能超过 500kb!')
      }
      return isIMAGE && isLt1M
    },
    // 删除上传的图片
    handleRemove(file, fileList) {
      this.form.personUphoto = null
      this.deletePhoto = true
    },
    // 图片的放大预览
    handlePictureCardPreview(file) {
      this.phone = true
    },
    // 图片的上传调用方法
    myUpload(file) {
      this.formData = file.file;
    },

    /** 提交按钮 */
    submitForm() {

      this.$refs["form"].validate(valid => {
        let formData = new FormData();
        formData.append("fileNow", this.deletePhoto);
        formData.append("file", this.formData);
        formData.append("personId", this.form.personId)
        formData.append("personUname", this.form.personUname)
        formData.append("personUphoto", this.form.personUphoto)
        formData.append("personName", this.form.personName)
        formData.append("personGender", this.form.personGender)
        formData.append("personAge", this.form.personAge)
        formData.append("personTel", this.form.personTel)
        formData.append("personNumber", this.form.personNumber)
        formData.append("personUtext", this.form.personUtext)
        formData.append("personAddress", this.form.personAddress)

        if (valid) {
          if (this.form.personId != null) {
            updatePerson(formData).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPerson(formData).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const personIds = row.personId || this.ids;
      this.$confirm('是否确认删除用户信息编号为"' + personIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPerson(personIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPerson(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style>
/*去除upload组件过渡效果*/
.el-upload-list__item {
  transition: none !important;
}
</style>
